package com.ess.project.examine.personnel.controller;

import com.ess.common.constant.UserConstants;
import com.ess.common.utils.poi.ExcelUtil;
import com.ess.framework.aspectj.lang.annotation.Log;
import com.ess.framework.aspectj.lang.enums.BusinessType;
import com.ess.framework.web.controller.BaseController;
import com.ess.framework.web.domain.AjaxResult;
import com.ess.framework.web.page.TableDataInfo;
import com.ess.project.examine.personnel.domain.ExaminePersonnelEntity;
import com.ess.project.examine.personnel.service.IPersonnelService;
import com.ess.project.system.role.domain.Role;
import com.ess.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考核人员管理
 *
 * @author daihui
 */
@Controller
@RequestMapping("/examine/personnel")
public class PersonnelController extends BaseController {
    private String prefix = "examine/personnel";

    @Autowired
    private IPersonnelService personnelService;


    @RequiresPermissions("examine:personnel:view")
    @GetMapping()
    public String user() {
        return prefix + "/personnel";
    }

    @RequiresPermissions("examine:personnel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExaminePersonnelEntity personnel) {
        startPage();
        List<ExaminePersonnelEntity> list = personnelService.selectPersonnelList(personnel);
        return getDataTable(list);
    }

    @Log(title = "考核人员管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("examine:personnel:export")
    @GetMapping("/export")
    @ResponseBody
    public void export(ExaminePersonnelEntity personnel, HttpServletResponse response) {
        personnelService.export(personnel, response);
    }


    @RequiresPermissions("examine:personnel:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ExaminePersonnelEntity> util = new ExcelUtil<>(ExaminePersonnelEntity.class);
        return util.importTemplateExcel("考核人员数据");
    }

    @Log(title = "考核人员管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("examine:personnel:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ExaminePersonnelEntity> util = new ExcelUtil<>(ExaminePersonnelEntity.class);
        List<ExaminePersonnelEntity> userList = util.importExcel(file.getInputStream());
        String message = personnelService.importPersonnel(userList, updateSupport);
        return AjaxResult.success(message);
    }



    /**
     * 新增考核人员
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存考核人员
     */
    @RequiresPermissions("examine:personnel:add")
    @Log(title = "考核人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExaminePersonnelEntity personnel) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(personnelService.checkPersonnelNameUnique(personnel.getExamPersonnel()))) {
            return error("新增考核人员'" + personnel.getExamPersonnel() + "'失败，改考核人员已存在");
        }
        return toAjax(personnelService.insertPersonnel(personnel));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{personnelId}")
    public String edit(@PathVariable("personnelId") Integer personnelId, ModelMap mmap) {
        ExaminePersonnelEntity personnel = personnelService.selectPersonnelById(personnelId);
        mmap.put("personnel", personnel);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("examine:personnel:edit")
    @Log(title = "考核人员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ExaminePersonnelEntity personnel) {
        return toAjax(personnelService.updatePersonnel(personnel));
    }

    /**
     * 校验考核人员姓名
     */
    @PostMapping("/checkPersonnelNameUnique")
    @ResponseBody
    public String checkPersonnelNameUnique(ExaminePersonnelEntity personnel) {
        return personnelService.checkPersonnelNameUnique(personnel.getExamPersonnel());
    }


    @RequiresPermissions("examine:personnel:remove")
    @Log(title = "考核人员管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(personnelService.deletePersonnelByIds(ids));
    }


    /**
     * 考核人员状态修改
     */
    @Log(title = "考核人员管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ExaminePersonnelEntity personnel) {
        return toAjax(personnelService.changeStatus(personnel));
    }
}
